package ext.acae2.change.ui;

import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import wt.util.WTException;
import wt.util.WTMessage;

import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationFeedbackMsg;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.acae2.ACAEConstants;
import ext.acae2.change.resource.ECNResource;

/**
 * @desription 判断Task是否成功创建或者编辑成功
 * @author cnie
 * @date 2013-6-13 下午3:48:51
 */
public class TaskSuccessValidator extends DefaultUIComponentValidator {
	
	private static final String RESOURCE = ECNResource.class.getName();

	@Override
	public UIValidationResult validateFormSubmission(
			UIValidationKey uivalidationkey,
			UIValidationCriteria uivalidationcriteria, Locale locale)
			throws WTException {
		UIValidationResult uivalidationresult = UIValidationResult.newInstance(
				uivalidationkey, UIValidationStatus.PERMITTED);
		String errorStr = null;
		String taskvalidatorFlag = uivalidationcriteria.getTextParameter("taskvalidatorFlag");
		String ecnType = uivalidationcriteria.getComboBox().get("createType").get(0);
		if(StringUtils.isNotBlank(ecnType)){
			if (ecnType.indexOf(ACAEConstants.ACAE2_TYPE_FILE_ECN) > -1
					|| ecnType.indexOf(ACAEConstants.ACAE2_TYPE_PARTDRAWING_ECN) > -1||ecnType.indexOf(ACAEConstants.ACAE2_TYPE_FILE_OBSOLETE_ECN)>-1) {
				if(StringUtils.isNotBlank(taskvalidatorFlag)){
					boolean successFlag = Boolean.valueOf(taskvalidatorFlag);
					if(!successFlag){
						errorStr = WTMessage.getLocalizedMessage(RESOURCE, ECNResource.TASK_CREATE_ERROR, null,locale);
					}	
				}
			}
		}
		
		if(StringUtils.isNotBlank(errorStr)){
			uivalidationresult.setStatus(UIValidationStatus.DENIED);
			uivalidationresult.addFeedbackMsg(UIValidationFeedbackMsg.newInstance(errorStr, FeedbackType.ERROR));
		}
		return uivalidationresult;

	}

}
